home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / Networking / TCP / TCPSend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.2 KB  |  132 lines  |  [TEXT/MPS ]

  1. /*
  2. ** James "im" Beninghaus
  3. */
  4.  
  5. #include    <QuickDraw.h>
  6. #include    <Devices.h>
  7. #include    <stdio.h>
  8. #include    <string.h>
  9.  
  10. #include    <MacTCPCommonTypes.h>
  11. #include    <AddressXlation.h>
  12. #include    <GetMyIPAddr.h>
  13. #include    <TCPPB.h>
  14. #include    <UDPPB.h>
  15.  
  16. #define        _STORAGE_    true
  17. #include    <TCP.h>
  18.  
  19. main (int argc, char *argv[]) {
  20.     
  21.     auto    OSErr            osErr            = noErr;
  22.     auto    short            index;
  23.     auto    char            *option;
  24.     auto    char            *parameter;
  25.     auto    char            *address;
  26.     auto    char            *text;
  27.     auto    TCPNotifyProc    asrProc            = nil;
  28.     auto    TCPiopb            pb;
  29.     auto    StreamPtr        stream;
  30.     auto    char            streamBuf[4096];
  31.     auto    long            streamBufLen    = sizeof(streamBuf);
  32.     auto    Ptr                streamBufPtr;
  33.     auto    ip_addr            localIP            = cAnyIP;
  34.     auto    ip_port            localPort        = cAnyPort;
  35.     auto    ip_addr            remoteIP        = cAnyIP;
  36.     auto    ip_port            remotePort        = cReceivePort;
  37.     auto    WDS(1)            wds;
  38.     
  39.     InitGraf((Ptr) &qd.thePort);
  40.  
  41.     if (argc > 1) {
  42.         index = 1;
  43.         while (index < argc) {
  44.         
  45.             option = argv[index++];
  46.             parameter = argv[index++];
  47.             
  48.             if ('-' != option[0] || (! strchr("atn", option[1])) || index > argc) {
  49.                 printf("TCPSend -a ipAddress -t text [-n]");
  50.                 exit(paramErr);
  51.             } else {
  52.                 switch (option[1]) {
  53.                     case 'a' :
  54.                         address = parameter;
  55.                         break;
  56.                     case 't' :
  57.                         text = parameter;
  58.                         break;
  59.                     case 'n' :
  60.                         asrProc = ASR;
  61.                         index--;
  62.                         break;
  63.                 }
  64.             }            
  65.         }
  66.     }
  67.     
  68.     osErr = _TCPInit();
  69.     if (noErr == osErr) {
  70.         osErr = _TCPCreate(&pb, &stream, streamBuf, streamBufLen, (TCPNotifyProc) asrProc, (Ptr) nil,  (TCPIOCompletionProc) nil, false);
  71.         if (noErr == osErr) {
  72.             osErr = TCPDotAddress(address, &remoteIP);
  73.             if (noErr == osErr) {
  74.                 osErr = _TCPActiveOpen(&pb, stream, remoteIP, remotePort, &localIP, &localPort, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  75.                 if (noErr == osErr) {
  76.                 
  77.                     /* send data to the remote host */
  78.                     wds.block[0].ptr    = text;
  79.                     wds.block[0].length    = strlen(text) + 1;
  80.                     wds.zero            = nil;
  81.                     osErr = _TCPSend(&pb, stream, (wdsEntry *) &wds, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  82.                     
  83.                     osErr = _TCPClose(&pb, stream, nil, (TCPIOCompletionProc) nil, false);
  84.                 }
  85.             }
  86.         }
  87.         osErr = _TCPRelease(&pb, stream, &streamBufPtr, &streamBufLen, (TCPIOCompletionProc) nil, false);
  88.     }
  89.     exit(osErr);
  90. }
  91.  
  92.  
  93. pascal void    StrToAddrResultProc(aHostInfo, userdata)    /* utility routine for StrToAddr */
  94.     struct hostInfo    *aHostInfo;
  95.     Ptr                userdata;
  96. {
  97.     /* simply watch the aHostInfo.rtnCode! */
  98. }
  99.  
  100. OSErr TCPDotAddress(char *dotAddress, ip_addr *ipAddress) {
  101.     auto    OSErr                osErr;
  102.     auto    struct hostInfo        aHostInfo;            /* a data structure for the DNR function */
  103.  
  104.     osErr = OpenResolver((char *) 0);
  105.     if (osErr) {
  106.         return osErr;
  107.     }
  108.     
  109.     /* ask the DNR function to get the IP address */
  110.     StrToAddr(dotAddress, &aHostInfo, (ResultProcPtr) StrToAddrResultProc, (Ptr) 0);
  111.     
  112.     /* wait for the address information or some error other than cacheFault to occur */
  113.     while (cacheFault == aHostInfo.rtnCode)
  114.         ;
  115.     
  116.     osErr = CloseResolver();
  117.     if (osErr) {
  118.         return osErr;
  119.     }
  120.  
  121.     /* if it was an error there isn't much more we can do here but let the caller know */
  122.     if (noErr != aHostInfo.rtnCode) {
  123.         osErr = aHostInfo.rtnCode;
  124.         return osErr;
  125.     }
  126.     
  127.     /* use the first IP address for this host */
  128.     *ipAddress = aHostInfo.addr[0];
  129.                 
  130.     return osErr;
  131. }
  132.